package cur.channel.client.netty;

import cur.channel.client.netty.NettyManager.NettyEntity;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
 * 对服务器链接引用的Map key=serverId:serverPort
 * Created by gantianxing on 2015/12/15.
 */
public class NettyServerMap {

    /**
     * 有效的服务器端连接
     */
    private static Map<String, NettyEntity> validServerMap =new ConcurrentHashMap<String, NettyEntity>();

    /**
     * 无效的服务器链接，需定时重新链接
     */
    private static Map<String, NettyEntity> invalidServerMap =new ConcurrentHashMap<String, NettyEntity>();

    /**
     * 添加有效的服务器连接
     * @param serverInfo
     * @param socketChannel
     */
    public static void addValid(String serverInfo, NettyEntity socketChannel){
        for (Map.Entry<String, NettyEntity> entry: validServerMap.entrySet()){
            if(entry.getKey().equals(serverInfo)){
                return;
            }
        }
        validServerMap.put(serverInfo,socketChannel);
    }

    /**
     * 取指定的有效的服务器链接
     * @param serverInfo
     * @return
     */
    public static NettyEntity getValid(String serverInfo){
        if(validServerMap.size() > 0){
            for (Map.Entry<String, NettyEntity> entry: validServerMap.entrySet()){
                if(entry.getKey().equals(serverInfo)){
                    return entry.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 随机取一个有效的服务器链接
     * @return
     */
    public static NettyEntity getRandomChannl(){
        NettyEntity randomValue =null;
        Set<String> keys = validServerMap.keySet();
        if(keys !=null && keys.size() >0){
            String[] keyArray = keys.toArray(new String[keys.size()]);
            Random random = new Random();
            String randomKey = keyArray[random.nextInt(keyArray.length)];
            randomValue = validServerMap.get(randomKey);
        }
        return randomValue;
    }

    public static Map<String, NettyEntity> getAllValid(){
        return validServerMap;
    }

    /**
     * 从有效的服务器链接中移除
     * @param serverInfo
     */
    public static void removeValid(String serverInfo){
        for (Map.Entry<String, NettyEntity> entry: validServerMap.entrySet()){
            if(entry.getKey().equals(serverInfo)){
                validServerMap.remove(serverInfo);
            }
        }
    }

    /**
     * 根据连接删除
     * @param channel
     * @return
     */
    public static Map.Entry<String, NettyEntity> remove(Channel channel){
        Map.Entry<String, NettyEntity> ret = null;
        NettyEntity clientInfo = null;
        for (Map.Entry<String, NettyEntity> entry: validServerMap.entrySet()){
            clientInfo = entry.getValue();
            if (clientInfo == channel){
                validServerMap.remove(entry.getKey());
                ret = entry;
                break;
            }
        }
        return ret;
    }

    /**
     * 添加无效的服务器连接
     * @param serverInfo
     */
    public static void addInValid(String serverInfo, NettyEntity socketChannel){
        for (Map.Entry<String, NettyEntity> entry: invalidServerMap.entrySet()){
            if(entry.getKey().equals(serverInfo)){
                return;
            }
        }
        invalidServerMap.put(serverInfo,socketChannel);
    }

    /**
     * 取指定的无效的服务器链接
     * @param serverInfo
     * @return
     */
    public static NettyEntity getInvalid(String serverInfo){
        for (Map.Entry<String, NettyEntity> entry: invalidServerMap.entrySet()){
            if(entry.getKey().equals(serverInfo)){
                return invalidServerMap.get(serverInfo);
            }
        }
        return null;
    }

    /**
     * 取所有的无效的服务器链接
     * @return
     */
    public static Map<String, NettyEntity> getAllInvalid(){
        return invalidServerMap;
    }

    /**
     * 移除无效的服务器链接
     * @param serverInfo
     */
    public static void removeInvalid(String serverInfo){
        for (Map.Entry<String, NettyEntity> entry: invalidServerMap.entrySet()){
            if(entry.getKey().equals(serverInfo)){
                invalidServerMap.remove(serverInfo);
            }
        }
    }



}